import 'package:flutter/material.dart';
import 'package:flutter_app/openeye/config/color.dart';
import 'package:flutter_app/openeye/config/string.dart';

enum ViewState { loading, done, error }

class LoadingStateWidget extends StatelessWidget {
   ViewState viewState;
   VoidCallback? retry;
   Widget child;

   LoadingStateWidget(
      {Key? key, this.viewState = ViewState.loading, this.retry,required this.child})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (viewState == ViewState.loading) {
      return _loadView;
    } else if (viewState == ViewState.error) {
      return _errorView;
    } else {
      return child;
    }
  }

  Widget get _loadView => const Center(
        child: CircularProgressIndicator(),
      );

  Widget get _errorView {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            'images/ic_error.png',
            width: 100,
            height: 100,
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
            child: Text(
              LeoString.net_request_fail,
              style: TextStyle(color: LeoColor.hitTextColor, fontSize: 18),
            ),
          ),
           Padding(padding: const EdgeInsets.only(top: 8),child:
          OutlinedButton(
            onPressed: retry,
            child: const Text(
              LeoString.reload_again,
              style: TextStyle(color: Colors.black87),
            ),
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(Colors.white),
              overlayColor: MaterialStateProperty.all(Colors.black12),
            ),
          )
            ,),
        ],
      ),
    );
  }
}
